package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatTeamCategoryTag;
import com.happy.cat.project.system.service.ICatTeamCategoryTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 品类标签Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/teamCategoryTag" )
@Api("品类标签 API")
public class CatTeamCategoryTagController extends BaseController {

    private final ICatTeamCategoryTagService iCatTeamCategoryTagService;

    /**
     * 导出品类标签列表
     */
    @GetMapping("/load" )
    public AjaxResult load() {
        return AjaxResult.success(iCatTeamCategoryTagService.list());
    }

    /**
     * 查询品类标签列表
     */
    @ApiOperation(value = "查询品类标签列表",notes = "查询品类标签列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategoryTag:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatTeamCategoryTag catTeamCategoryTag)
    {
        startPage();
        LambdaQueryWrapper<CatTeamCategoryTag> lqw = new LambdaQueryWrapper<CatTeamCategoryTag>();
        if (catTeamCategoryTag.getItemId() != null){
            lqw.eq(CatTeamCategoryTag::getItemId ,catTeamCategoryTag.getItemId());
        }
        if (StringUtils.isNotBlank(catTeamCategoryTag.getTagType())){
            lqw.eq(CatTeamCategoryTag::getTagType ,catTeamCategoryTag.getTagType());
        }
        if (StringUtils.isNotBlank(catTeamCategoryTag.getIcon())){
            lqw.eq(CatTeamCategoryTag::getIcon ,catTeamCategoryTag.getIcon());
        }
        if (StringUtils.isNotBlank(catTeamCategoryTag.getName())){
            lqw.like(CatTeamCategoryTag::getName ,catTeamCategoryTag.getName());
        }
        if (StringUtils.isNotBlank(catTeamCategoryTag.getPosition())){
            lqw.eq(CatTeamCategoryTag::getPosition ,catTeamCategoryTag.getPosition());
        }
        if (catTeamCategoryTag.getSortId() != null){
            lqw.eq(CatTeamCategoryTag::getSortId ,catTeamCategoryTag.getSortId());
        }
        if (catTeamCategoryTag.getCreateDate() != null){
            lqw.eq(CatTeamCategoryTag::getCreateDate ,catTeamCategoryTag.getCreateDate());
        }
        if (catTeamCategoryTag.getStatus() != null){
            lqw.eq(CatTeamCategoryTag::getStatus ,catTeamCategoryTag.getStatus());
        }
        List<CatTeamCategoryTag> list = iCatTeamCategoryTagService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出品类标签列表
     */
    @ApiOperation(value = "导出品类标签列表",notes = "导出品类标签列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategoryTag:export')" )
    @Log(title = "品类标签" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatTeamCategoryTag catTeamCategoryTag) {
        LambdaQueryWrapper<CatTeamCategoryTag> lqw = new LambdaQueryWrapper<CatTeamCategoryTag>(catTeamCategoryTag);
        List<CatTeamCategoryTag> list = iCatTeamCategoryTagService.list(lqw);
        ExcelUtil<CatTeamCategoryTag> util = new ExcelUtil<CatTeamCategoryTag>(CatTeamCategoryTag. class);
        return util.exportExcel(list, "teamCategoryTag" );
    }

    /**
     * 获取品类标签详细信息
     */
    @ApiOperation(value = "获取品类标签的标签列表",notes = "获取品类标签的标签列表",response = AjaxResult.class)
    @GetMapping(value = "/byItem" )
    public AjaxResult findByItemId(Long itemId, String tagType) {
        LambdaQueryWrapper<CatTeamCategoryTag> lqw = Wrappers.lambdaQuery(CatTeamCategoryTag.class)
                .eq(CatTeamCategoryTag::getItemId, itemId).eq(CatTeamCategoryTag::getTagType, tagType);
        return AjaxResult.success(iCatTeamCategoryTagService.list(lqw));
    }

    /**
     * 获取品类标签详细信息
     */
    @ApiOperation(value = "获取品类标签详细信息",notes = "获取品类标签详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategoryTag:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatTeamCategoryTagService.getById(id));
    }

    /**
     * 新增品类标签
     */
    @ApiOperation(value = "新增品类标签",notes = "新增品类标签",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategoryTag:add')" )
    @Log(title = "品类标签" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatTeamCategoryTag catTeamCategoryTag) {
        catTeamCategoryTag.setCreateDate(new Date());
        catTeamCategoryTag.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatTeamCategoryTagService.save(catTeamCategoryTag) ? 1 : 0);
    }

    /**
     * 修改品类标签
     */
    @ApiOperation(value = "修改品类标签",notes = "修改品类标签",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategoryTag:edit')" )
    @Log(title = "品类标签" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatTeamCategoryTag catTeamCategoryTag) {
        catTeamCategoryTag.setCreateDate(new Date());
        catTeamCategoryTag.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatTeamCategoryTagService.updateById(catTeamCategoryTag) ? 1 : 0);
    }

    /**
     * 删除品类标签
     */
    @ApiOperation(value = "删除品类标签",notes = "删除品类标签",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategoryTag:remove')" )
    @Log(title = "品类标签" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatTeamCategoryTagService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
